/*
 * @Description: 网管
 * @Author: Haor
 * @Date: 2022-05-06 16:12:52
 * @LastEditTime: 2022-05-06 16:57:21
 * @LastEditors: Haor
 */

import { IStrategyOptions, Strategy } from "passport-local";
import { PassportStrategy } from '@nestjs/passport'
import { Injectable, UnauthorizedException } from '@nestjs/common'
import { AuthService } from './auth.service'
import { User } from "@entities/user.entity";

@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
	constructor(private readonly authService: AuthService) {
		super({
            usernameField: 'userName',
            passwordField: 'password'
        } as IStrategyOptions)
	}

	async validate(username: string, password: string): Promise<User> {
		console.log('username>>>:', username)
		const user = await this.authService.validateUser({
			userName: username,
			password
		})
		if (!user) {
			throw new UnauthorizedException()
		}
		return user
	}
}
